package com.marion.jpaspringbootstarter.config;

import com.marion.jpaspringbootstarter.model.MUser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 默认创建一个User
 * @author Marion
 * @date 2022/3/2 18:54
 */
@Configuration
@EnableConfigurationProperties(MJpaProperties.class)
@ConditionalOnClass(MUser.class)
@ConditionalOnWebApplication
public class MJpaAutoConfiguration {

    @Bean
    @ConditionalOnMissingClass
    @ConditionalOnProperty(prefix = "mjpa", value = "enable", havingValue = "true")
    public MUser defaultUser(MJpaProperties properties) {
        return MUser.builder().id(properties.getId())
                .name(properties.getName())
                .build();
    }

}
